home *** CD-ROM | disk | FTP | other *** search
- From: tony@online.tmx.com.au (Tony Cook)
- Message-ID: <199603150208.MAA05447@online.tmx.com.au>
- X-Original-Date: Fri, 15 Mar 1996 12:08:35 +1000
- Path: in2.uu.net!bounce-back
- Date: 15 Mar 96 02:13:29 GMT
- Approved: fjh@cs.mu.oz.au
- Return-Path: <daemon@meeker.UCAR.EDU>
- Newsgroups: comp.std.c++
- Subject: Re: Problem with new in templates...
- Organization: Home
- References: <adamnash-1303962301470001@gel.stanford.edu>
- X-Newsreader: TIN [version 1.2 PL2]
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMUjSUuEDnX0m9pzZAQExRQF+IndIP3xgdWyRVYlfkQXf/xG4Z50G3Ism
- vVXjZGlyIgkp0z62qDJP0NSavJGArybh
- =QiI2
-
- Adam Nash (adamnash@cs.stanford.edu) wrote:
- : OK Problem Scenario:
-
- : There is a templatized data structure that is based on type DATA. This
- : data structure features a method that depends on a method in type DATA.
- : Thus, for semantic reasons, the template assumes that DATA is a pointer
- : type.
- :...
- : The problem occurs on the second line. data is of type DATA, so you
- : really want to allocate a new (*DATA). IE, if DATA is a CBar *, then data
- : is a CBar *,
- : so we want it to say: data = new CBar;
-
- : How can I do this, only knowing the pointer type?
- : IE, is there a version of new that can take the pointer type, rather than
- : the actual type? RTTI is fair game.
-
- How about:
- template <class T>
- T* new_of_ptr(T * /* ignored */)
- {
- return new T;
- }
-
- ...
- DATA value = new_of_ptr((DATA)0);
- ...
-
- --
- Tony Cook - tony@online.tmx.com.au
- 100237.3425@compuserve.com
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-